
package com.rt.schedulenew.web;
import com.github.pagehelper.Page;
import com.rt.schedulebase.dto.*;
import com.rt.schedulenew.api.*;
import com.rt.schedulenew.utils.global.PageVO;
import com.github.pagehelper.PageHelper;
import com.rt.schedulebase.entity.DictRoleFunction;
import com.rt.schedulebase.entity.DictFunction;
import com.rt.schedulebase.entity.DictRole;
import com.rt.schedulebase.entity.DictUserRole;
import java.util.List;
import java.util.stream.Collectors;

import com.rt.schedulenew.utils.checkparam.config.CheckOne;
import com.rt.schedulenew.utils.checkparam.config.Check;
import com.rt.schedulenew.utils.global.JsonResultUtil;
import com.rt.schedulebase.entity.DictUsers;
import org.springframework.web.bind.annotation.PostMapping;
import com.rt.schedulenew.utils.global.JsonResult;
import org.springframework.web.bind.annotation.RequestBody;
import com.rt.schedulenew.api.IDictSerialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping({ "/user" })
public class UserController
{
    @Autowired
    IDictUsersService dictUsersService;
    @Autowired
    IDictUserRoleService dictUserRoleService;
    @Autowired
    IDictRoleService dictRoleService;
    @Autowired
    ICommonService commonService;
    @Autowired
    IDictRoleFunctionService dictRoleFunctionService;
    @Autowired
    IDictFunctionService dictFunctionService;
    @Autowired
    IExamPatientLockService examPatientLockService;
    @Autowired
    IExamApplyService examApplyService;
    @Autowired
    IDictSerialService dictSerialService;
    
    @PostMapping({ "/list" })
    public JsonResult getUserList(@RequestBody DictUsersDto dto) {
        return dictUsersService.getListPage(dto);
    }
    
    @PostMapping({ "/insert" })
    @Check({ "userId", "staffNo" })
    public JsonResult insertNewUser(@RequestBody DictUsers dictUsers) {
        return dictUsersService.insertUser(dictUsers);
    }
    
    @PostMapping({ "/update" })
    @Check({ "password" })
    @CheckOne({ "userId", "staffNo" })
    public JsonResult updateUser(@RequestBody DictUsersDto dictUsersDto) {
        dictUsersService.update(dictUsersDto);
        return JsonResultUtil.success();
    }
    
    @PostMapping({ "/delete" })
    @Check({ "userId" })
    public JsonResult deleteUser(@RequestBody DictUsers dictUsers) {
        dictUsersService.delete(dictUsers);
        return JsonResultUtil.success();
    }
    
    @PostMapping({ "/userRole/list" })
    @Check({ "userId" })
    public JsonResult userRoleList(@RequestBody DictUserRoleDto dto) {
        List<DictUserRoleDto> list = dictUserRoleService.getUserRoleList(dto);
        return JsonResultUtil.success(list);
    }
    
    @PostMapping({ "/userRole/insert" })
    @Check({ "userId", "roleId" })
    public JsonResult insertUserRole(@RequestBody DictUserRole dictUserRole) {
        boolean bool = dictUserRoleService.insertRoleBatch(dictUserRole);
        if (bool) {
            return JsonResultUtil.success();
        }
        return JsonResultUtil.failure();
    }
    
    @PostMapping({ "/userRole/delete" })
    @Check({ "userId", "roleId" })
    public JsonResult deleteUserRole(@RequestBody DictUserRole dictUserRole) {
        boolean deleteResult = dictUserRoleService.delete(dictUserRole);
        if (deleteResult) {
            return JsonResultUtil.success("删除成功");
        }
        return JsonResultUtil.failure("删除失败");
    }
    
    @PostMapping({ "/userRole/insertList" })
    @Check({ "userId" })
    public JsonResult userRoleListCanInsert(@RequestBody DictUserRoleDto dto) {
        List<DictUserRoleDto> list = dictUserRoleService.getUserRoleListForInsert(dto);
        return JsonResultUtil.success(list);
    }
    
    @PostMapping({ "/role/list" })
    public JsonResult getRoleList(@RequestBody DictRoleDto dto) {
        List<DictRole> list = dictRoleService.getList(dto);
        return JsonResultUtil.success(list);
    }
    
    @PostMapping({ "/role/update" })
    @Check({ "roleId" })
    public JsonResult updateRole(@RequestBody DictRole dictRole) {
        DictRoleDto dictRoleDto = new DictRoleDto();
        dictRoleDto.setRoleName(dictRole.getRoleName());
        List<DictRole> list = dictRoleService.getList(dictRoleDto);
        list = list.stream().filter(item -> item.getRoleName().equals(dictRole.getRoleName())).collect(Collectors.toList());
        if (list == null || list.size() < 1) {
            boolean bool = dictRoleService.update(dictRole);
            if (bool) {
                return JsonResultUtil.success();
            }
            return JsonResultUtil.failure();
        } else {
            if (list.size() == 1
                    && list.stream().filter(item -> item.getRoleId().equals(dictRole.getRoleId())).collect(Collectors.toList()).size() > 0) {
                return JsonResultUtil.success();
            }
            return JsonResultUtil.failure("此用角色名称已被占用！");
        }
    }

    @PostMapping({ "/role/insert" })
    @Check({ "roleName" })
    public JsonResult insertNewRole(@RequestBody DictRole dictRole) {
        return dictRoleService.insertRole(dictRole);
    }
    
    @PostMapping({ "/role/delete" })
    @Check({ "roleId" })
    public JsonResult deleteRole(@RequestBody DictRole dictRole) {
        boolean bool = dictRoleService.delete(dictRole);
        if (bool) {
            return JsonResultUtil.success();
        }
        return JsonResultUtil.failure();
    }
    
    @PostMapping({ "/role/function/list" })
    @Check({ "roleId" })
    public JsonResult getRoleFunctionList(@RequestBody DictRoleFunctionDto dto) {
        List<DictRoleFunctionDto> list = dictRoleFunctionService.getRoleFunctionList(dto);
        return JsonResultUtil.success(list);
    }
    
    @PostMapping({ "/role/function/insertList" })
    @Check({ "roleId" })
    public JsonResult roleFunctionListCanInsert(@RequestBody DictFunctionDto dto) {
        List<DictFunction> list = dictFunctionService.getFunctionListForInsert(dto);
        return JsonResultUtil.success(list);
    }
    
    @PostMapping({ "/role/function/insert" })
    @Check({ "roleId", "funId" })
    public JsonResult insertFunction(@RequestBody DictRoleFunction dictRoleFunction) {
        boolean bool = dictRoleFunctionService.insertBatch(dictRoleFunction);
        if (bool) {
            return JsonResultUtil.success();
        }
        return JsonResultUtil.failure();
    }
    
    @PostMapping({ "/role/function/delete" })
    @Check({ "roleId", "funId" })
    public JsonResult deleteFunction(@RequestBody DictRoleFunction dictRoleFunction) {
        boolean bool = dictRoleFunctionService.deleteByCondition(dictRoleFunction);
        if (bool) {
            return JsonResultUtil.success();
        }
        return JsonResultUtil.failure();
    }
    
    @PostMapping({ "/role/staff/list" })
    @Check({ "roleId" })
    public JsonResult roleUserList(@RequestBody DictUserRoleDto dto) {
        List<DictUserRoleDto> list = dictUserRoleService.getRoleUserList(dto);
        return JsonResultUtil.success(list);
    }
    
    @PostMapping({ "/role/staff/insertList" })
    @Check({ "roleId" })
    public JsonResult roleUserListCanInsert(@RequestBody DictUserRoleDto dto) {
        List<DictUserRoleDto> list = dictUserRoleService.getRoleUserListForInsert(dto);
        return JsonResultUtil.success(list);
    }
    
    @PostMapping({ "/role/staff/insert" })
    @Check({ "userId", "roleId" })
    public JsonResult insertRoleUser(@RequestBody DictUserRole dictUserRole) {
        boolean bool = dictUserRoleService.insertUserBatch(dictUserRole);
        if (bool) {
            return JsonResultUtil.success();
        }
        return JsonResultUtil.failure();
    }
    
    @PostMapping({ "/role/staff/delete" })
    @Check({ "userId", "roleId" })
    public JsonResult deleteRoleUser(@RequestBody DictUserRole dictUserRole) {
        boolean deleteResult = dictUserRoleService.delete(dictUserRole);
        if (deleteResult) {
            return JsonResultUtil.success("删除成功");
        }
        return JsonResultUtil.failure("删除失败");
    }
    
    @PostMapping({ "/blacklist/list" })
    public JsonResult getBlackList(@RequestBody ExamPatientLockDto dto) {
        Page<?> page = PageHelper.startPage(dto.getPageNo(), dto.getPageSize());
        List<ExamPatientLockDto> list = examPatientLockService.selectList(dto);
        PageVO pageVO = new PageVO();
        pageVO.setPageNo(dto.getPageNo());
        pageVO.setPageSize(dto.getPageSize());
        pageVO.setTotal(page.getTotal());
        pageVO.setPages(page.getPages());
        return JsonResultUtil.success("成功", list, pageVO);
    }
    
    @PostMapping({ "/blacklist/unlock" })
    public JsonResult unLockPatient(@RequestBody ExamPatientLockDto dto) {
        return examPatientLockService.unLockPatient(dto);
    }
    
    @PostMapping({ "/updatePassword" })
    @Check({ "password", "oldPassword" })
    @CheckOne({ "userId", "staffNo" })
    public JsonResult updatePassword(@RequestBody DictUsersDto dictUsersDto) {
        boolean bool = dictUsersService.update(dictUsersDto);
        if (bool) {
            return JsonResultUtil.success();
        }
        return JsonResultUtil.failure("原密码不正确");
    }

    @PostMapping({ "/setPrinter" })
    public JsonResult setPrinter(@RequestBody DictUsersDto dictUsersDto) {
        boolean bool = dictUsersService.setPrinter(dictUsersDto);
        if (bool) {
            return JsonResultUtil.success();
        }
        return JsonResultUtil.failure("打印机保存失败");
    }



}
